package com.benyanyi.okhttp;

import android.content.Context;

import com.benyanyi.okhttp.type.HttpRequest;
import com.benyanyi.okhttp.type.RequestType;
import com.benyanyi.okhttp.util.FormatUtil;

import java.io.File;

/**
 * @author YanYi
 * @date 2019-08-02 22:22
 * @email ben@yanyi.red
 * @overview
 */
public class OkHttpUtil {

    private static OkHttpUtil instance;
    private static Context mContext;

    public static File cacheFile;

    public static OkHttpUtil getInstance(Context context) {
        if (instance == null) {
            instance = new OkHttpUtil();
        }
        mContext = context;
        return instance;
    }

    public RequestType url(String url) {
        if (FormatUtil.isEmpty(url) || (url.indexOf("https:") != 0 && url.indexOf("http:") != 0)) {
            throw new RuntimeException("url is not http or https");
        }
        return new HttpRequest(mContext, url, false);
    }

    public RequestType url(String url, boolean isCache) {
        if (FormatUtil.isEmpty(url) || (url.indexOf("https:") != 0 && url.indexOf("http:") != 0)) {
            throw new RuntimeException("url is not http or https");
        }
        return new HttpRequest(mContext, url, isCache);
    }

}
